/*
 *  NFCSigning - Open source library for signing/validation of NDEF messages
 *  Copyright (C) 2009-2010 The NFCSigning Team
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 */
package sampleappa;

//#ifdef MyJSR257
//# import my.javax.microedition.contactless.ndef.NDEFMessage;
//# import my.javax.microedition.contactless.ndef.NDEFRecord;
//#else
import javax.microedition.contactless.ndef.NDEFMessage;
import javax.microedition.contactless.ndef.NDEFRecord;
//#endif

import org.bouncycastle.asn1.x509.X509Name;

/**
 *
 * @author emarkki
 */
class Utils {
       
    public static void dump(NDEFMessage msg) {
        StringBuffer sb = new StringBuffer();
        sb.append("\nDump:\n");
        sb.append("bytes: " + msg.toByteArray().length + "\n");
        sb.append("numberOfRecords: " + msg.getNumberOfRecords() + "\n");
        sb.append("records.length: " + msg.getRecords().length + "\n");
        
        for(int i = 0; i < msg.getNumberOfRecords(); i++) {
            NDEFRecord r = msg.getRecord(i);
            sb.append("record " + i);
            sb.append(", type = " + r.getRecordType().getName());
            sb.append(", id = "+toBinaryString(r.getId()));
            sb.append(", payloadLen = "+r.getPayloadLength());
            sb.append(", payload = "+toBinaryString(r.getPayload()));
            sb.append(", payload = "+toFixedHexString(r.getPayload()));
        }
        
        System.out.println(sb.toString());
        System.out.println("msg = " + toBinaryString(msg.toByteArray()));
    }
    
    public static String toBinaryString(byte[] bytes) {
        if(bytes == null) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i < bytes.length; i++) {
            String zero = "00000000";
            String bin = Integer.toBinaryString(((int)bytes[i])&0xff);
            sb.append(zero.substring(bin.length()));
            sb.append(bin + " ");
        }
        return sb.toString();
    }
    
    public static String toFixedSize(String s, int length) {
        StringBuffer sb = new StringBuffer();
        sb.append(s);
        for(int i = s.length(); i < length; i++) {
            sb.append(" ");
        }
        return sb.toString();
    }
    
    public static String toHexString(byte[] bytes) {
        return toHexString(bytes, false);
    }
    
    public static String toHexString(byte[] bytes, boolean space) {
        if(bytes == null) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i < bytes.length; i++) {
            String zero = "00";
            String hex = Integer.toHexString((int)bytes[i]&0xff);
            sb.append(zero.substring(hex.length()));
            sb.append(hex);
            if(space) {
                sb.append(" ");
            }
        }
        return sb.toString();
    }
    
    /**
     * There is a bug here somewhere
     * @param bytes
     * @return
     */
    public static String toFixedHexString(byte[] bytes) {
        if(bytes == null) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i < bytes.length; i++) {
            sb.append(toFixedSize(Integer.toHexString(bytes[i]), 8) + " ");
        }
        return sb.toString();
    }
    
    public static void dumpAsJavaByteArray(byte[] bytes) {
        dumpAsJavaByteArray(bytes, 4);                
    }
    
    public static void dumpAsJavaByteArray(byte[] bytes, int cols) {
        System.out.println("/** " + bytes.length + " bytes */");
        System.out.println("byte[] message = { ");
        
        for(int i = 0; i < bytes.length; i++) {
            System.out.print("\t(byte) " + (bytes[i] & 0xff) + ", ");
            if(i%cols == 0) {
                System.out.println();
            }
        }
        System.out.println();
        System.out.println("};");
                
    }
    
    public static boolean equals(byte[] bytes1, byte[] bytes2) {
        if(bytes1 == null && bytes2 == null) {
            return true;
        } else if(bytes1 == null || bytes2 == null) {
            return false;
        }
        
        if(bytes1.length != bytes2.length) {
            return false;
        }
        
        for(int i = 0; i < bytes1.length; i++) {
            if(bytes1[i] != bytes2[i]) {
                return false;
            }
        }
        
        return true;
    }
    
    public static boolean equals(int[] int1, int[] int2) {
        if(int1 == null && int2 == null) {
            return true;
        } else if(int1 == null || int2 == null) {
            return false;
        }
        
        if(int1.length != int2.length) {
            return false;
        }
        
        for(int i = 0; i < int1.length; i++) {
            if(int1[i] != int2[i]) {
                return false;
            }
        }
        
        return true;
    }

    static boolean equals(byte[] bytes1, int offset, int length, byte[] bytes2) {
        if(bytes1 == null && bytes2 == null) {
            return true;
        } else if(bytes1 == null || bytes2 == null) {
            return false;
        }
        
        if(length != bytes2.length) {
            return false;
        }
        
        for(int i = offset, j=0; j < length; i++,j++) {
            if(bytes1[i] != bytes2[j]) {
                return false;
            }
        }
        
        return true;
    }
    
    public static String getCommonName(X509Name name) {
        String s = name.toString();
        if(s.indexOf("CN=") != -1) {
            s = s.substring(s.indexOf("CN=")+3);
            if(s.indexOf(",") != -1) {
                s = s.substring(0, s.indexOf(","));
            }
        }
        return s;
    }
}
